<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property string $user_id
 * @property string $username
 * @property string $password
 * @property string $name
 * @property integer $state
 * @property integer $type
 * @property integer $created_at
 *
 * The followings are the available model relations:
 * @property Issue[] $issues
 * @property IssueReply[] $issueReplies
 * @property Learn[] $learns
 * @property Lesson[] $lessons
 * @property QuestionBank[] $questionBanks
 * @property Score[] $scores
 * @property Thread[] $threads
 * @property Topic[] $topics
 */
class User extends CActiveRecord
{
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{user}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, password, type, created_at', 'required'),
            array('username', 'unique', 'message' => '账号已存在,换一个吧!'),
            array('state, type, created_at', 'numerical', 'integerOnly' => true),
            array('username, name', 'length', 'max' => 20),
            array('password', 'length', 'max' => 32),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('user_id, username, password, name, state, type, created_at', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'issues' => array(self::HAS_MANY, 'Issue', 'user_id'),
            'issueReplies' => array(self::HAS_MANY, 'IssueReply', 'user_id'),
            'learns' => array(self::HAS_MANY, 'Learn', 'user_id'),
            'lessons' => array(self::HAS_MANY, 'Lesson', 'user_id'),
            'questionBanks' => array(self::HAS_MANY, 'QuestionBank', 'user_id'),
            'scores' => array(self::HAS_MANY, 'Score', 'user_id'),
            'threads' => array(self::HAS_MANY, 'Thread', 'user_id'),
            'topics' => array(self::HAS_MANY, 'Topic', 'user_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'user_id' => 'User',
            'username' => '账号',
            'password' => '密码',
            'name' => '姓名',
            'state' => '状态',
            'type' => '角色',
            'created_at' => '创建时间'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('user_id', $this->user_id, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('state', $this->state);
        $criteria->compare('type', $this->type);
        $criteria->compare('created_at', $this->created_at);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    public function onBeforeValidate($event)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }
        parent::onBeforeValidate($event); // TODO: Change the autogenerated stub
    }
}
